package ourfilter;

import java.awt.image.renderable.ParameterBlock;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;

import ourImage.OurPlanarImage;
import pimpmypipe.filter.DataTransformationFilter;
import pimpmypipe.interfaces.Readable;
import pimpmypipe.interfaces.Writeable;
import pimpmypipe.pipe.Pipe;

/**
 * @author Meraner Marcel
 */
public class ThresholdFilter extends DataTransformationFilter<OurPlanarImage> {
    
    static {
        System.setProperty("com.sun.media.jai.disableMediaLib", "true");
    }
    
    private double _threshold = 50.0;
    private boolean _filterEnabled = false;
    
    
    public ThresholdFilter(Readable<OurPlanarImage> in) {
        super(in);
    }
    public ThresholdFilter(Readable<OurPlanarImage> in, Writeable<OurPlanarImage> out) {
        super(in, out);
    }
    
    
    public ThresholdFilter(Pipe<OurPlanarImage> in, Pipe<OurPlanarImage> out){
        super(in, out);
        in.setOut(this);
        out.setIn(this);
    }
    
    @Override
    protected void process(OurPlanarImage entity) {
        if (_filterEnabled) {
            
            double[] low, high, map;
            //1 for bright, 0 for dark
            int bands = 1;

            low = new double[bands];
            high = new double[bands];
            map = new double[bands];

            for (int i = 0; i < bands; i++) {
                low[i] = 0;
                high[i] = _threshold;
                map[i] = 255; //max wert
            }

            ParameterBlock pb = new ParameterBlock();
            pb.addSource(entity.getImage());
            pb.add(low);
            pb.add(high);
            pb.add(map);
            PlanarImage image = JAI.create("threshold", pb);

            low = new double[bands];
            high = new double[bands];
            map = new double[bands];

            for (int i = 0; i < bands; i++) {
                low[i] = 0;
                high[i] = 254;
                map[i] = 0; //max wert
            }

            ParameterBlock pb1 = new ParameterBlock();
            pb1.addSource(image);
            pb1.add(low);
            pb1.add(high);
            pb1.add(map);
            entity.setImage(JAI.create("threshold", pb1));
        }
    }

    public double getThreshold() {
        return _threshold;
    }

    public void setThreshold(double threshold) {
        _threshold = threshold;
    }

    public boolean isEnabled() {
        return _filterEnabled;
    }

    public void setFilterEnabled(boolean enabled) {
        _filterEnabled = enabled;
    }
    
    
}
